<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2022/3/21
 * Time: 15:56
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

namespace app\api\controller\v1;

use app\common\lib\ArrClass;
use app\common\model\BuddhaArticle;
use app\common\model\BuddhaArticleIndex;
use app\common\model\BuddhaBanner;
use app\common\model\BuddhaBuddha;
use app\common\lib\Show;
use app\common\lib\Status;
use app\common\model\DiyHome;
use app\common\model\MachineCost;
use app\common\model\MallBanner;
use app\common\model\MallSpecsValue;
use app\common\model\WeixinMenu;
use Htlove\Tree;

class Index extends Base
{
    public function getIndexByListInfo()
    {
        //按照机器编号查询机器
        $machine_id = $this->request->param('machine_id', '', 'trim');
        if (empty($machine_id)) {
            return Show::json_show(Status::mini_error, "机器错误");
        }
        $machine = (new MachineCost())->getMachineCostByMachineIdInfo($machine_id);
        if (empty($machine)) {
            return Show::json_show(Status::mini_error, "机器错误");
        }
        $machine_ids = array_column($machine, "specs_id");
        $specs_value = (new MallSpecsValue())->getSpecsValueByList($machine_ids, 'specs');
        if (empty($specs_value)) {
            return Show::json_show(Status::mini_error, "机器类型错误");
        }
        $machine_money = array_column($machine, 'money', 'specs_id');
        $machine_value = array_column($machine, 'value', 'specs_id');
        $machine_type = array_column($machine, 'type', 'specs_id');

        $lists = [];
        foreach ($specs_value as $k => $v) {
            $lists[$v['specs']['id']]['title'] = $v['specs']['title'];
            $lists[$v['specs']['id']]['type'] = $v['specs']['type'];
            $lists[$v['specs']['id']]['list'][] = [
                "id" => $v['id'],
                "title" => $v['title'],
                "money" => $machine_money[$v['id']],
                "type" => $machine_type[$v['id']],
                "checked" => false
            ];
        }

        return Show::json_show(Status::mini_normal, "ok", [
            "row" => $lists
        ]);
    }

    /**
     * @return array|\think\response\Json|void
     */
    public function getDiyHomeByList()
    {
        try {
            $diy = (new DiyHome())->getDiyHomeByList();
        } catch (\Exception $exception) {
            return Show::json_show(Status::mini_error, "error");
        }
        if ($diy) {
            $diy = $diy->toArray();
            $diy['content'] = Show::json_decode($diy['content']);
            return Show::json_show(Status::mini_normal, "ok", $diy);
        }
    }

    public function getUserLocationPol()
    {
        $lat = $this->request->param('lat', 0, 'floatval');
        $lng = $this->request->param('lng', 0, 'floatval');
        $type = sysconfig('map', 'map_type');
        $key = "map_" . $type . "_key_id";
        $sect = sysconfig("map", $key);
        $obj = ArrClass::initClass(sysconfig('map', 'map_type'), ArrClass::mapAttr(), [$sect], true);
        if (empty($lat)) {

            $adr = $obj->getIpAddress($this->request->ip());
            if ($adr['code'] != Status::status_normal) {
                return Show::json_show(Status::mini_error, $adr['msg']);
            }
            $lat = $adr['data']['location']['lat'];
            $lng = $adr['data']['location']['lng'];
        }
        $menu = $this->request->param('menu', '', 'trim');
        $page = $this->request->param('page', 1, 'intval');
        $cate_title = $this->request->param('cate_title');
        $lists = (new WeixinMenu())->getBaseByListAll(['status' => 1], '*', 'sort desc,id desc');
        if (empty($menu)) {
            //先搜索标题
            $menu = $lists[0]['value'];
        }


        $res = $obj->search($menu, $lat, $lng, $page, 1000, $cate_title);

        if ($res['code'] == 1) {
            return Show::json_show(Status::mini_normal, 'ok', [
                'address' => $res['data'],
                'title' => $menu,
            ]);
        } else {
            return Show::json_show(Status::mini_error, $res['msg']);
        }
    }

    /**
     * @return array|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getIndexCateByList()
    {
        $pid = $this->request->param('pid', 0, 'intval');
        $model_obj = new WeixinMenu();
        $menu = $model_obj->where('pid', 0)->order('sort desc ,id desc')->select()->toArray();
        $child = [];
        foreach ($menu as $v) {
            if ($v['id'] == $pid) {
                //查询下一级
                $child = $model_obj->where('pid', $pid)->order('sort desc,id desc')->select()->toArray();
            }
        }
        $child = array_merge([[
            "id" => 0,
            "title" => "全部",
            "value" => ""
        ]], $child);
        return Show::json_show(Status::mini_normal, 'ok', [
            "menu" => $menu,
            "child" => $child
        ]);
    }


    public function getBannerListInfo()
    {
        $type = $this->request->param('type', 1, 'intval');
        $where = [
            ['type', '=', $type]
        ];
        try {
            $lists = (new MallBanner())->getBaseByNormalList($where, 'id,title,image');
        } catch (\Exception $e) {
            return Show::json_show(Status::status_error, $e->getMessage());
        }
        return Show::json_show(Status::mini_normal, "ok", $lists);
    }
}